AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
--include('entities/base_wire_entity/init.lua')
include( 'shared.lua' )

function ENT:Initialize()

	self.Entity:SetModel( "models/Items/CrossbowRounds.mdl" )
	self.Entity:SetName("Fuse")
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
    	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableGravity(true)
		phys:EnableDrag(false)
		phys:EnableCollisions(true)
		phys:SetMass( 1 )
	end
	
	self.Segments = {}
	self.Sparks = {}
	
	for x = -11,11 do
		self.Segments[x] = 0
	end	
	
	self.Spd = 0
	self.LTT = 0
end

function ENT:Think()
	local Delta = CurTime() - self.LTT
	if self:IsOnFire() && !self.Sparked then
		for x = -11,11 do
			self:Sparky(x,math.Rand(-1,1))
		end
	end
	
	for k,e in pairs(self.Sparks) do
		--PrintTable(e)
		local effectdata = EffectData()
		effectdata:SetOrigin( self:GetPos() + self:GetForward() * e.Pos )
		effectdata:SetScale( 1 )
		effectdata:SetMagnitude( 1 )
		util.Effect( "Sparks", effectdata )	
		
		util.BlastDamage(self.Entity, self.Entity, self:GetPos() + (self:GetForward() * e.Pos) + ( self:GetUp() * 0 ), 1, 1 )
		
		e.Pos = e.Pos + (e.Dir * Delta)
		
		if e.Pos > 11 || e.Pos < -11 then
			table.remove(self.Sparks,k)
		end
	end
	
	self.LTT = CurTime()
	
	if self.Plugging && !self.Plugged then
		if self.PEnt && self.PEnt:IsValid() then
			self:SetPos(self:GetPos() + self:GetForward() * (CustomSign(self.Spd) * 6) )
			self:SetParent(self.PEnt)
			--local w = constraint.Weld(self,self.PEnt)
		end
		self.Plugged = true
		self.Plugging = false
	end
	
	self.Entity:NextThink( CurTime() + 0.1 ) 
	return true	
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16 + Vector(0,0,50)
	
	local ent = ents.Create( "Fuse" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Initialize()
	ent:Activate()
	ent.SPL = ply
	
	return ent
	
end

function ENT:OnTakeDamage( info )
	--print(info:GetDamageType())
	if info:GetInflictor() == self then
		--print("Self Damage Detected")
		return
	end
	local Pos = self:GetForward():DotProduct( self:GetPos() - info:GetDamagePosition() ) * -1
	local Dir = 0
	if Pos >= 8 then
		Pos = 11
		Dir = -3
		
		self:Sparky(Pos,Dir)
	elseif Pos <= -8 then
		Pos = -11
		Dir = 3
		
		self:Sparky(Pos,Dir)
	
	else
		self:Sparky(Pos,-3)
		self:Sparky(Pos,3)
	end
end

function ENT:Use( activator, caller )
	
end

function ENT:Touch( ent )
	if !self.Plugged && !ent.Fused then
		--ent:SetFuse( self )
	end
	
end

function ENT:PhysicsCollide( data, physobj )
	if data.HitEntity:IsOnFire() then
		--print( self:GetForward():DotProduct( self:GetPos() - data.HitPos ) )
		local Pos = self:GetForward():DotProduct( self:GetPos() - data.HitPos ) * -1
		local Dir = 0
		if Pos >= 8 then
			Pos = 11
			Dir = -3
			
			self:Sparky(Pos,Dir)
		elseif Pos <= -8 then
			Pos = -11
			Dir = 3
			
			self:Sparky(Pos,Dir)
		
		else
			self:Sparky(Pos,-3)
			self:Sparky(Pos,3)
		end
		
	else
		if !data.HitEntity:IsWorld() then
			local Spd = self:GetForward():DotProduct( data.OurOldVelocity )
			
			print(Spd)
			
			if Spd <= -100 || Spd >= 100 then
				self.Plugging = true
				self.PEnt = data.HitEntity
				self.Spd = Spd
				
				print("Plugging")
			end
		end		
	end
end

function ENT:Sparky( Pos, Dir )
	local t = {}
	t.Pos = Pos
	t.Dir = Dir
	if table.Count(self.Sparks) <=5 then
		table.insert(self.Sparks,t)
	end
end

function CustomSign(var)
	if var > 0 then 
		return 1
	elseif var < 0 then 
		return -1
	else
		return 0
	end
end